<template>
  <div>
    <el-select v-model="schoolId" placeholder="请选择" size="mini" style="width:200px;margin-right:20px" clearable>
      <el-option
        v-for="item in schArr"
        :key="item.value"
        :label="item.label"
        :value="item.value">
      </el-option>
    </el-select>
    <el-button size="mini" type="primary" @click="add()">新增</el-button>
    <el-table :data="tableData1" border="" style="width: 100%;margin-top: 20px" v-loading="loading">
      <el-table-column prop="schoolName" label="学校名称" align="center" />
      <el-table-column label="是否开课" align="center" width="150" >
        <template slot-scope="scope">
          <span>
            启用
          </span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="150">
        <template slot-scope="scope">
          <!-- <span v-if="state == 0">
            <el-button type="primary" size="mini" @click="open(scope.row)" :disabled='true'>启用</el-button>
            <el-button type="danger" size="mini" @click="close(scope.row)">禁用</el-button>
          </span>
          <span v-else>
            <el-button type="primary" size="mini" @click="open(scope.row)" >启用</el-button>
            <el-button type="danger" size="mini"  @click="close(scope.row)" :disabled='true'>禁用</el-button>
          </span> -->
          <el-button type="danger" size="mini" @click="close(scope.row.schoolId)">禁用</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="parms.pageNum"
      :page-sizes="[5, 10, 10, 20]"
      :page-size="parms.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      background
      style="margin-top: 20px"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    >
    </el-pagination>
    <!-- <div slot="footer" class="dialog-footer" style="margin-top: 20px">
      <el-button type="primary" size="mini">完成</el-button>
      <el-button type="warning" size="mini">暂存</el-button>
      <el-button @click="handCancle" size="mini">取消</el-button> 
    </div> -->
  </div>
</template>

<script>
import { getList, getSch } from "@/api/school/school"
import { selSchool, updateStatus } from '@/api/course/standard'
export default {
  props: ['id', 'showDialog'],
  data() {
    return {
      input: '',
      form: {
        name: ''
      },
      value: true,
      options: [],
      switch1: false,
      tableData: [],
      tableData1: [],
      total: null,
      loading: false,
      parms:{
        pageNum: 1,
        pageSize: 5,
        shcoolName: ''
      },
      state: 0,
      schArr: [],
      schArr1: [],
      schoolId: null
    }
  },
  created() {
    this.getList()
  },
  methods: {
    add() {
      if(this.schoolId == null){
        this.$message.error('请选择学校')
        return
      }
      updateStatus(this.id, this.schoolId, 0).then(res => {
        if(res.code === 200){
          this.$message.success('新增成功')
          this.getList()
        }
      })
    },
    close(id) {
      console.log(id)
      console.log(this.id)
      updateStatus(this.id, id, 1).then(res => {
        if(res.code === 200){
          this.$message.success('禁用成功')
          this.getList()
        }
      })
    },
    getList() {
      this.loading = true
      // getList(this.parms.pageNum,this.parms.pageSize,this.parms.shcoolName).then(res => {
      //   this.tableData = res.rows
      //   this.loading = false
      //   this.total = res.total
      // })
      selSchool(this.id).then(res => {
        // let arr = res.data
        // let arr1 = []
        // for(let i=0;i<arr.length;i++){
        //   arr1.push(arr[i].schoolId)
        // }
        // this.schArr = arr1
        // console.log(this.schArr)
        this.tableData1 = res.rows
        this.loading = false
        this.total = res.total
      })
      getSch().then(res => {
        let arr = res.data
        let arr1 = []
        for(let i=0;i<arr.length;i++){
          arr1.push({'value': arr[i].id, 'label': arr[i].schoolName})
        }
        this.schArr = arr1
      })
    },
    changContractType() {
      alert(123)
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.parms.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.parms.pageNum = val
      this.getList()
    }
  },
  watch: {
    showDialog: function(n, o) {
      if (n) {
        this.getList()
        console.log(this.id)
      }
    }
  }
}
</script>

<style>
.flex{
  display: flex;
  justify-content: space-between;
}
.el-table th.gutter {
  display: table-cell !important
}
</style>